package com.stnet.dao.enterprice.chinamobile;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.modules.commons.Pager;
import com.stnet.bases.BaseDaoImpl;
import com.stnet.entity.Transfer;

/**
 * @author Messcat
 * @version 1.1
 * 
 */
public class TransferDaoImpl extends BaseDaoImpl implements TransferDao {

	public TransferDaoImpl() {
	}

	public void delete(Transfer transfer) {
		getHibernateTemplate().delete(transfer);
	}

	public void delete(Long id) {
		getHibernateTemplate().delete(get(id));
	}

	public List findAll() {
		List find = getHibernateTemplate().find("from Transfer");
		return find;
	}

	public void save(Transfer transfer) {
		getHibernateTemplate().save(transfer);
	}

	public void update(Transfer transfer) {
		getHibernateTemplate().merge(transfer);
	}

	public Transfer get(Long id) {
		return (Transfer) getHibernateTemplate().get(Transfer.class, id);
	}

	public List<Transfer> findTransferByAgent(Long agent) {
		List find = getHibernateTemplate().find("from Transfer where user.id = ? order by id desc", agent);
		return find;
	}

	public Pager getListByAgent(int pageSize, int pageNo, Long agent) {
		Session session = null;
		Pager pager = null;

		session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Transfer.class);

		criteria.add(Restrictions.eq("user.id", agent));
		criteria.addOrder(Order.desc("editedate"));
		int rowCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
		criteria.setProjection(null);
		int startIndex = pageSize * (pageNo - 1);
		criteria.setFirstResult(startIndex);
		criteria.setMaxResults(pageSize);
		List result = criteria.list();
		pager = new Pager(pageSize, pageNo, rowCount, result);

		session.close();

		return pager;
	}

}
